package com.lemote.action.order;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lemote.action.BaseAction;
import com.lemote.core.PropertyFilter;
import com.lemote.entity.CircuitBoard;
import com.lemote.entity.FlowTask;
import com.lemote.entity.FtReport;
import com.lemote.entity.OrderEntity;
import com.lemote.entity.ShowEntity;
import com.lemote.entity.User;
import com.lemote.exception.AppException;
import com.lemote.service.order.CircuitBoardService;
import com.lemote.service.order.FlowTaskService;
import com.lemote.service.order.FtReportService;
import com.lemote.service.order.ShowEntityService;
import com.lemote.utils.Constant;

/**
 * 
 * 
 * 
 * 
 * @author slaton.wu@gmail.com
 * @version 1.0
 * @created 2011-7-31 下午01:42:13
 */
@Service("ftReportAction")
public class FtReportAction extends BaseAction<FtReport, FtReportService> {

	private FtReport ftReport;

	private CircuitBoardService circuitBoardService;

	private FlowTaskService flowTaskService;
	
	private ShowEntityService showEntityService;

	@Override
	public FtReport getModel() {

		return ftReport;
	}

	@Override
	public String list() throws AppException {

		List<PropertyFilter> filters = PropertyFilter
				.buildFromHttpRequest(request);
		page = entityService.search(page, filters);
		return SUCCESS;
	}

	@Override
	public String input() throws AppException {

		return null;
	}

	@Override
	public String save() throws AppException {
		setBackUrl("board/ftList.htm");
		String bid = request.getParameter("bid");
		if (bid != null && ftReport != null) {
			FlowTask task = flowTaskService.getTaskByTaskName("repair");
			User tester = (User) request.getSession().getAttribute(
					Constant.SESSION_USER);
			Long cid = Long.parseLong(bid);
			CircuitBoard board = circuitBoardService.get(cid);
			ftReport.setBoard(board);
			ftReport.setJobName(board.getFlowTask().getTaskName());
			ftReport.setTestDate(new Date());
			ftReport.setTester(tester);
			entityService.add(ftReport);
			board.setPreTask(board.getFlowTask());
			board.setFlowTask(task);
			board.setStatus("1");
			ShowEntity show = showEntityService.get("orderNo", board.getOrderNo());
			if (board.getRtimes()<=0) {
				show.setFtRepairNum(show.getFtRepairNum()+1);
			}
			showEntityService.update(show);
			dealBoard(board);
			circuitBoardService.update(board);

			return GLOBALSUCCESS;
		}
		return ERROR;
	}

	/**
	 * 
	 * 板子出现不良时更新订单及板子信息
	 * 
	 * @param board
	 */
	private void dealBoard(CircuitBoard board) {
		OrderEntity oe = board.getOrderEntity();
		if (board.getRtimes() != null && board.getRtimes() == 0) {
			oe.setThrough(oe.getThrough() - 1);
		}
		oe.setBadNum(oe.getBadNum() + 1);
		board.setOrderEntity(oe);
		board.setRtimes(board.getRtimes() + 1);
	}

	@Override
	public String delete() throws AppException {

		return null;
	}

	@Override
	public void prepareModel() throws AppException {
		if (id != null) {
			ftReport = entityService.get(id);
		} else {
			ftReport = new FtReport();
		}

	}

	@Autowired
	@Override
	public void setEntityService(FtReportService entityService) {

		this.entityService = entityService;

	}

	public FtReport getFtReport() {
		return ftReport;
	}

	public void setFtReport(FtReport ftReport) {
		this.ftReport = ftReport;
	}

	@Autowired
	public void setCircuitBoardService(CircuitBoardService circuitBoardService) {
		this.circuitBoardService = circuitBoardService;
	}

	@Autowired
	public void setFlowTaskService(FlowTaskService flowTaskService) {
		this.flowTaskService = flowTaskService;
	}
	@Autowired
	public void setShowEntityService(ShowEntityService showEntityService) {
		this.showEntityService = showEntityService;
	}
	
	

}
